package com.javadaemon.Tinderbox.loader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.javadaemon.Tinderbox.Constants;
import com.javadaemon.Tinderbox.Tinderbox;
import com.javadaemon.Tinderbox.model.Misc;

/**
 * This file is part of Tinderbox.
 *
 * Tinderbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ponix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tinderbox.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This class opens Jagex's non-javascript page and grabs paramaters.
 * @author Mads Horndrup
 */
public class ParamaterParser {

    private ArrayList<String> lines = new ArrayList<String>();
    private HashMap<String, String> params = new HashMap<String, String>();

    public ParamaterParser() {
        try {
            readPage(new URL(Constants.GAME_URL));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ParamaterParser readPage() throws MalformedURLException {
        return readPage(new URL(Tinderbox.URL));
    }

    public String parseURL() {
        for (String line : lines) {
            if (line.contains("<frame style")) {
                return line.substring(line.indexOf("src=") + 5, line.indexOf("\" width="));
            }
        }
        return "";
    }

    public ParamaterParser readPage(URL url) {
        try {
            URLConnection connection = url.openConnection();
            connection.addRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            connection.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            connection.addRequestProperty("Accept-Encoding", "gzip,deflate");
            connection.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
            connection.addRequestProperty("Connection", "keep-alive");
            connection.addRequestProperty("Host", "www.runescape.com");
            connection.addRequestProperty("Keep-Alive", "300");
            connection.addRequestProperty("User-Agent", Misc.getHttpUserAgent());
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            lines.clear();
            while ((line = reader.readLine()) != null) {
                lines.add(line.trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    public HashMap<String, String> parseParamaters() {
        Pattern pattern = Pattern.compile("<param name=\"?([^\\s]+)\"?\\s+value=\"?([^>]*)\"?>");
        for (String line : lines) {
            Matcher matcher = pattern.matcher(line);
            if (matcher.matches()) {
                String param_name = Misc.trim(matcher.group(1), '\"');
                String param_value = Misc.trim(matcher.group(2), '\"');
                params.put(param_name, param_value);
            }
        }
        if (params.containsKey("haveie6")) {
            params.remove("haveie6");
            params.put("haveie6", "0");
        }
        return params;
    }
}
